package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.service.MemeberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;

import javax.jws.Oneway;
import java.math.BigDecimal;
import java.util.*;

/**
 * 会员服务业务逻辑处理层
 *
 * @author wangxin
 * @version 1.0
 */
@Service(interfaceClass = MemeberService.class)
@Transactional
public class MemeberServiceImpl implements MemeberService {

    @Autowired
    private MemberDao memberDao;

    /**
     * 查询所有用户的年龄,并以年龄段封装到Map集合中
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getAllAge() {
        int count18 = 0;//0<x<=18
        int count30 = 0;//18<x<=30
        int count45 = 0;//30<x<=45
        int countover45 = 0;//>45
        //查询到所有的生日
        List<String> allAges = memberDao.getAllAge();
        for (String ageStr : allAges) {
            if (ageStr != null) {
                int age = Integer.parseInt(ageStr);
                if (age > 0 && age <= 18) {
                    count18++;
                }
                if (age > 18 && age <= 30) {
                    count30++;
                }
                if (age > 30 && age <= 45) {
                    count45++;
                }
                if (age > 45) {
                    countover45++;
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();
        Map<String, Object> map4 = new HashMap<>();
        map.put("name", "0-18岁");
        map.put("value", count18);
        map2.put("name", "18-30岁");
        map2.put("value", count30);
        map3.put("name", "30-45岁");
        map3.put("value", count45);
        map4.put("name", "45岁以上");
        map4.put("value", countover45);
        List<Map<String, Object>> list = new ArrayList<>();
        list.add(map);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        return list;
    }

    /**
     * 根据年月获取累计会员数量
     *
     * @param listMonth
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonth(List<String> listMonth) {
        //定义list集合存放会员数量
        List<Integer> memberCount = new ArrayList<>();
        if (listMonth != null && listMonth.size() > 0) {
            for (String yearMonth : listMonth) {
                //2020-02 select count(*) from t_member where regTime <= '2020-02-31'
                yearMonth = yearMonth + "-31";
                Integer count = memberDao.findMemberCountBeforeDate(yearMonth);
                memberCount.add(count);
            }
        }
        return memberCount;
    }

    //查询所有用户的生日
    @Override
    public List<Map<String, Object>> getAllbirthday() {
        //设置返controller的值
        List<Map<String, Object>> list = new ArrayList<>();
        //查询到所有的生日
        List<Date> allBirthday = memberDao.getAllBirthday();

        Double count = allBirthday.size() * 1.0;//总人数
        Calendar calendar = Calendar.getInstance();//获取当前的日历对象
        int year = calendar.get(Calendar.YEAR);//获取到今年
        int month = calendar.get(Calendar.MONTH);//获取当月
        int day = calendar.get(Calendar.DAY_OF_MONTH);//获取到当月的当前天数

        //把所有的数据变成日历对象
        List<Calendar> calendars = new ArrayList<>();
        for (Date date : allBirthday) {
            if (date != null) {
                Calendar calendarforMysql = Calendar.getInstance();
                calendarforMysql.setTime(date);
                calendars.add(calendarforMysql);
            }
        }
        //做年龄段判断
        int count18 = 0;//0<x<=18
        int count30 = 0;//18<x<=30
        int count45 = 0;//30<x<=45
        int countover45 = 0;//>45
        for (Calendar calendar1 : calendars) {
            int year1 = calendar1.get(Calendar.YEAR);//数据库获取的date数据的年份
            int month1 = calendar1.get(Calendar.MONTH);//数据库获取的date数据的月份
            int day1 = calendar1.get(Calendar.DAY_OF_MONTH);//数据库获取的date数据的日子
            if (year - year1 <= 18 && year - year1 > -1) {
                if (month1 - month == 0) {
                    if (day1 - day < 0) {
                        count30++;
                    }
                } else if (month1 - month < 0) {
                    count30++;
                } else {
                    count18++;
                }
            } else if (year - year1 <= 30 && year - year1 > 18) {
                if (month1 - month == 0) {
                    if (day1 - day < 0) {
                        count45++;
                    }
                } else if (month1 - month < 0) {
                    count45++;
                } else {
                    count30++;
                }
            } else if (year - year1 <= 45 && year - year1 > 30) {
                if (month1 - month == 0) {
                    if (day1 - day < 0) {
                        countover45++;
                    }
                } else if (month1 - month < 0) {
                    countover45++;
                } else {
                    count45++;
                }
            } else {
                countover45++;
            }
        }

       /* //查询占比
        Double lt18 = count18/count;//0<x<=18
        Double lt30 = count30/count;//18<x<=30
        Double lt45 = count45/count;//30<x<=45
        Double over45 = countover45/count;//>45*/

        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();
        Map<String, Object> map4 = new HashMap<>();
        map.put("name", "0-18岁");
        map.put("value", count18);
        map2.put("name", "18-30岁");
        map2.put("value", count30);
        map3.put("name", "30-45岁");
        map3.put("value", count45);
        map4.put("name", "45岁以上");
        map4.put("value", countover45);


        list.add(map);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        return list;
    }

    //按照性别来分类会员
    @Override
    public List<Map<String, Object>> getMembersDate() {
        //查询数据库中会员的性别和数量
        List<Map<String, Object>> sexDateFormysql = memberDao.findAllMemberSexAndCount();
        //从数据库中得到的性别是1 2 更改值
        for (Map<String, Object> map : sexDateFormysql) {
            String name = (String) map.get("name");
            if (name != null) {
                if (name.equals("1")) {
                    map.put("name", "男");
                } else if (name.equals("2")) {
                    map.put("name", "女");
                }
            }
        }
        return sexDateFormysql;
    }


}
